package com.nineclock.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.document.dto.DocUserCollaborationDTO;
import com.nineclock.document.dto.DocumentUserDTO;
import com.nineclock.document.mapper.DocCollaborationsMapper;
import com.nineclock.document.mapper.DocFileMapper;
import com.nineclock.document.pojo.DocCollaborations;
import com.nineclock.document.pojo.DocFile;
import com.nineclock.document.service.DocCollaborationsService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysCompanyUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.naming.NotContextException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class DocCollaborationsServiceImpl implements DocCollaborationsService {
    @Autowired
    private SysCompanyUserFeign sysCompanyUserFeign;
    @Autowired
    private DocFileMapper docFileMapper;
    @Autowired
    private DocCollaborationsMapper docCollaborationsMapper;

    @Override
    public PageResult<DocumentUserDTO> pagingCollaborations(Long id, Integer page, Integer pageSize) {
        if (id == 0 || page == 0 || pageSize == 0){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        IPage<DocCollaborations> ipage = new Page<>(page, pageSize);

        LambdaQueryWrapper<DocCollaborations> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocCollaborations::getFileId,id);
        ipage = docCollaborationsMapper.selectPage(ipage, wrapper);
        List<DocCollaborations> docCollaborationsList = ipage.getRecords();

        List<DocumentUserDTO> documentUserDTOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(docCollaborationsList)) {
            documentUserDTOList = docCollaborationsList.stream().map(docCollaborations -> {
                DocumentUserDTO documentUserDTO = new DocumentUserDTO();
                documentUserDTO.setId(docCollaborations.getCollaborationId());
                SysCompanyUserDTO companyUserDTO = sysCompanyUserFeign.queryCompanyUserById(docCollaborations.getCollaborationId()).getData();
                if (companyUserDTO != null) {
                    documentUserDTO.setUserName(companyUserDTO.getUserName());
                }
                return documentUserDTO;
            }).collect(Collectors.toList());
        }

        return new PageResult<>(ipage.getTotal(),ipage.getPages(),documentUserDTOList);
    }

    @Override
    public void insertCollaboration(Map<String, Object> map) {
        if (map.size() == 0){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        Integer fileId = (Integer) map.get("fileId");
        Integer userId = (Integer) map.get("userId");
        DocCollaborations docCollaborations = new DocCollaborations();
        docCollaborations.setFileId(Long.valueOf(fileId));
        docCollaborations.setCollaborationId(Long.valueOf(userId));

        docCollaborationsMapper.insert(docCollaborations);
    }

    @Override
    public void deleteCollaboration(Map<String, Object> map) {
        if (map.size() == 0){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        Integer fileId = (Integer) map.get("fileId");
        Integer userId = (Integer) map.get("userId");
        LambdaQueryWrapper<DocCollaborations> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocCollaborations::getFileId,fileId);
        wrapper.eq(DocCollaborations::getCollaborationId,userId);
        docCollaborationsMapper.delete(wrapper);

    }

    @Override
    public List<DocUserCollaborationDTO> pagingUsers(Long id) {
        if (id == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        DocFile docFile = docFileMapper.selectById(id);
        List<SysCompanyUserDTO> companyUserDTOList = sysCompanyUserFeign.queryAllUser().getData();

        LambdaQueryWrapper<DocCollaborations> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocCollaborations::getFileId,id);
        List<DocCollaborations> docCollaborations = docCollaborationsMapper.selectList(wrapper);

        List<Long> idList = docCollaborations.stream().map(docCollaboration -> {
            Long collaborationId = docCollaboration.getCollaborationId();
            return collaborationId;
        }).collect(Collectors.toList());

        List<DocUserCollaborationDTO> docUserCollaborationDTOList = companyUserDTOList.stream().map(sysCompanyUserDTO -> {
            DocUserCollaborationDTO docUserCollaborationDTO = new DocUserCollaborationDTO();
            docUserCollaborationDTO.setId(sysCompanyUserDTO.getId());
            docUserCollaborationDTO.setUsername(sysCompanyUserDTO.getUserName());
            docUserCollaborationDTO.setImgUrl(sysCompanyUserDTO.getImageUrl());
            docUserCollaborationDTO.setPhone(sysCompanyUserDTO.getMobile());

            if (docFile.getCreateUserId() == sysCompanyUserDTO.getId()){
                docUserCollaborationDTO.setState(1);
            }else if (idList.contains(sysCompanyUserDTO.getId())){
                docUserCollaborationDTO.setState(2);
            }else {
                docUserCollaborationDTO.setState(0);
            }
            return docUserCollaborationDTO;

        }).collect(Collectors.toList());
        return docUserCollaborationDTOList;

    }
}
